package Stack;

import java.util.Scanner;

public class ArrayStatckDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        String key = "";
        boolean loop = true;
         arrayStack.push(1);
         arrayStack.push(2);
         arrayStack.push(3);
         arrayStack.push(4);
         arrayStack.push(5);
         arrayStack.list();
         System.out.println("出栈");
         int pop = arrayStack.pop();
        System.out.printf("出栈的是%d",pop);
    }

}
class ArrayStack{
    private int maxSize;//栈的大小
    private int[] stack;
    private int top = -1;//栈顶，初始化为-1

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }

    public boolean isFull(){
        return  top == maxSize - 1;
    }
    public boolean isEmpty(){
        return top == -1;
    }
    public void push(int value){
        if (isFull()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }
    public int pop(){
        //先判断栈是否空
        if (isEmpty()){
            throw new RuntimeException("栈空，没有数据");

        }
        int value = stack[top];
        top--;
        return value;
    }
    public void list(){
        if (isEmpty()){
            System.out.println("栈空，没有数据");
            return;
        }
        for (int i = top; i >=0 ; i--) {
            System.out.printf("stack[%d] = %d\n",i,stack[i]);
        }
    }
}